﻿var banks = [];
var bankPromotions = [];

function LoanCalculatorUI(obj) {

    this.initialUI = function (func) {
        //jQuery.support.cors = true;
        $.ajax({
            url: '/api/bank/GetBanks',
            type: 'GET',
            dataType: 'json',
            controlTarget: jQuery('#BankName'),
            success: function (data) {
                bindDataToUl(jQuery('#BankName'), data);
                banks = data;
                if (func != undefined) {
                    func();
                }
            },
            error: function (x, y, z) {
                console.trace(x + '\n' + y + '\n' + z);
            }
        });
    };

    


    this.notify = function (title, data, image, sticky, duration, class_name) {
        image = typeof image !== 'undefined' ? image : 'img/avatar-mini.jpg';
        sticky = typeof sticky !== 'undefined' ? sticky : true;
        duration = typeof duration !== 'undefined' ? duration : '';
        class_name = typeof class_name !== 'undefined' ? class_name : 'my-sticky-class';

        $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: title,
            // (string | mandatory) the text inside the notification
            text: data,
            // (string | optional) the image to display on the left
            image: image,
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: sticky,
            // (int | optional) the time you want it to be alive for before fading out
            time: duration,
            // (string | optional) the class name you want to apply to that specific message
            class_name: class_name
        });
    };
}

function bindDataToUl(select, data) {
    var items = [];
    items.push('<option value="default">ระบุธนาคาร</option>');
    $.each(data, function (id, option) {
        items.push('<option value="' + option.Id + '">' + option.Name + '</option>');
    });
    select.html(items.join(''));
};

function bindingEvent() {

    this.initial = function(){
        jQuery('#BankName').on('change', function () {
            $.ajax({
                url: '/api/Loan/GetLoansByBank/' + $(this).attr('value'),
                type: 'GET',
                dataType: 'json',
                controlTarget: jQuery('#BankPromotion'),
                success: function (data) {
                    bankPromotions = data;
                    var items = [];
                    $.each(data, function (id, option) {
                        items.push('<option value="' + option.Id + '">' + option.Description + '</option>');
                    });
                    jQuery('#BankPromotion').html(items.join(''));
                    jQuery('#BankInterest').val(data[0].Interest)
                },
                error: function (x, y, z) {
                    console.trace(x + '\n' + y + '\n' + z);
                }
            });
        });
        jQuery('#BankPromotion').on('change', function () {
            var selectedValue = $(this).attr('value');
            $.ajax({
                url: '/api/Condition/GetConditionsByLoan/' + selectedValue,
                type: 'GET',
                dataType: 'json',
                controlTarget: jQuery('#BankInterest'),
                success: function (data) {
                    var items = [];
                    conditional.ClearCondition();
                    $.each(data, function (id, value) {
                        conditional.AddCondition(value.Interest, null, null, value.Period)
                    });
                    conditional.SetConditionDate(new Date());
                    conditional.PopulateConditionTable();
                    //jQuery('#BankInterest').val(bankPromotions[0].Interest)
                },
                error: function (x, y, z) {
                    console.trace(x + '\n' + y + '\n' + z);
                }
            });


            var loan =  $.grep(bankPromotions , function(n, i){
                return n.Id == selectedValue;
            });
            jQuery('#BankInterest').val(loan[0].Interest);
        });

        jQuery('#Principle').on('blur', function () {
            var principle = jQuery('#Principle').val();
            var minimum = ((principle * 0.66) / 100).toFixed(2);
            jQuery('#installmentMinimum').text(' ผ่อนขั้นต่ำต่อเดือน = ' + minimum + ' ระยะเวลา 30 ปี');
            jQuery('#Installment').val(minimum);
        });
        
    };
}

function ShowLoading() {

}